<?php
namespace app\admin\controller;
use think\Db;
class Goods extends Base
{
	public function lists(){


        $rec        = new \app\admin\model\GoodsRec();
        $rec_list   = $rec->lists(0);#推荐位

        //ajax查询数据
        if( is_post() ){

            $page     = input('page');
            $cat_id   = input('cat_id');
            $rec_id = input('rec_list');
            $status   = input('status');
            $title    = input('title');
            $min_time = input('min_time');
            $max_time = input('max_time');
            $is_baoyou = input('is_baoyou');
            
            
            //原生态sql查询
            $sql = "
                select
                    [1]
                from
                    goods a
                left join
                    goods_cat b
                on
                    a.cat_id = b.id
                where 
                    1 = ?
                ";
            $param = [1];
            if ( $cat_id != '100all' ) {
                $sql .= " and a.cat_id = ?";
                $param[] = $cat_id;
            }
            if ( $rec_id != '100all' ) {
                $sql .= " and a.rec_id = ?";
                $param[] = $rec_id;
            }
            if ( $status != '100all' ) {
                $sql .= " and a.status = ?";
                $param[] = $status;
            }
            if ( $is_baoyou != '100all' ) {
                $sql .= " and a.is_baoyou = ?";
                $param[] = $is_baoyou;
            }
            if ( !empty($title) ) {
                $sql .= " and a.title like ? ";
                $param[] = "%{$title}%";
            }
            if ( !empty($min_time) ) {
                $sql    .= " and a.create_time >= ?";
                $param[] = strtotime($min_time);
            }
            if ( !empty($max_time) ) {
                $sql    .= " and a.create_time <= ?";
                $param[] = strtotime($max_time);
            }
            //总条数
            $sql_count = str_replace("[1]", "count(*) as total", $sql);
            $total     = Db::query($sql_count, $param);
            $total     = $total[0]['total'];
            
            //分页数据
            $page_size   = c('page_size');     #分页条数
            $page        = com_cur_page($total, $page,  $page_size);   #当前页
			$page_data   = com_page_data($total, $page,  $page_size);   #分页原始数据
            
            //列表数据
			$start       = ($page - 1) * $page_size;
			$sql        .= " order by a.sort desc, a.id desc limit $start, $page_size";
			$field       = "a.*, b.cat_title ";
			$sql_list    = str_replace("[1]", $field, $sql);
            $list        = Db::query($sql_list, $param);


            //格式数据
            foreach($list as $k => $v){                
                $list[$k]['checkbox']       =  com_format_checkbox($v['id']);
                $list[$k]['create_time']    =  com_format_time( $v['create_time'] );
                $list[$k]['pic']            =  "<img src='{$v['pic']}' height='40' />";
                
                $rs                         =  [0=>'无推荐位'] + $rec_list;
                $list[$k]['rec_id']         =  com_format_select_edit( $v['rec_id'], $rs, url('goods/editRec'), $v['id'] );
                $list[$k]['views']          =  com_format_input_edit(url('goods/editViews'), $v['id'], $v['views']);
                $list[$k]['sort']           =  com_format_input_edit(url('ajax/editSort', 't=goods'), $v['id'], $v['sort']);
                
                $list[$k]['is_baoyou']      =  c( "type.is_baoyou" , $v['is_baoyou'] );

                $list[$k]['status']         =  com_format_status2(url('ajax/editStatus', 't=goods'),$v['id'],$v['status']);

                $list[$k]['coupon_url']     =  "<a target='_blank' href=".$v['coupon_url'].">优惠券链接</a>";
                $list[$k]['taobao_url']     =  "<a target='_blank' href=".$v['taobao_url'].">商品链接</a>";

                $list[$k]['coupon_kl']      =  "<input value=".$v['coupon_kl'].">";
                $list[$k]['taobao_kl']      =  "<input value=".$v['taobao_kl'].">";

                //操作按钮
                $list[$k]['action']         =  com_action_edit( url("goods/edit","id={$v['id']}"), $v['id'], '编辑商品' );
                $list[$k]['action']        .=  com_action_copy( url('ajax/copy','t=goods'), $v['id'], "你确定要拷贝[{$v['title']}]吗?" );
                $list[$k]['action']        .=  com_action_del( url("ajax/delete","t=goods"), $v['id'], "你确定要删除{$v['title']}吗？" );
                
                
            }

            $com_body    = com_build_table($list, array("checkbox","id","title", "pic","rec_id", "cat_title","is_baoyou","coupon_price","coupon_num", "coupon_url","taobao_url" , "coupon_kl","taobao_kl","quanhou_price","views", "action"));
            $com_page    = com_build_page( $page_data );
            $data = array(
                "com_body"  => $com_body,
                "com_page"  => $com_page,
            );
            
            $config = [
                'data'   => $data,
                'suc'    => [
                    'js_action' => 'function.switchs',
                ],
            ];
            com_check($config);
        }

        $all= [
            '100all' => [
                'id' => '100all',
                'parent_id' => 0,
                'cat_title' => '全部分类',
                'depath' => 0,
                ]
        ];
        $category = $all + \app\admin\model\GoodsCat::allSubCat();
        $rec        = new \app\admin\model\GoodsRec();
        $rec_list   = $rec->lists(0);#推荐位
        $data = [
            'nav'       => ['商品分类', '列表'],
            'category'  => $category,
            'status'    => ['100all'=>'全部状态'] + c('type.status'),
            'is_baoyou' => ['100all'=>'全部状态'] + c('type.is_baoyou'),
            'rec_list'  => ['100all'=>'全部推荐位'] + $rec_list,
        ];
        return view('', $data);
    }





    //添加商品
    public function add(){

        if( is_post() ){
            $data = input();  #post数据
            unset($data['pic']);
            unset($data['input-file']);
            
            //文件上传
            if( isset($_FILES['pic']) ){
                $pic = com_up_pic('article', 'pic', true);
                com_check( ['err'=>['js_action'=>'alert'], 'suc'=>['return_type'=>'return']]);
                $data['pic'] = $pic;
            }

            $data['create_time'] = time();
            

            $article  = new \app\admin\model\Goods();
            $article->save($data);
            $config = [
                'msg' => '添加商品成功！',
                'suc' => [
                    'js_action' => 'alert|close',
                ],
            ];
            com_check( $config );
        }    
        //模板赋值
        $rec    = new \app\admin\model\GoodsRec();
        $ac     = new \app\admin\model\GoodsCat();
        $all= [
            '100all' => [
                'id' => '100all',
                'parent_id' => 0,
                'cat_title' => '请选择分类',
                'depath' => 0,
                ]
        ];
        $category = $all + $ac::allSubCat();
        $data = [
            'category'  => $category,
            'rec'       => array_merge([0=>'无推荐位'], $rec->lists(0)),
            'status'    => c('type.status'),
            'is_baoyou' => c('type.is_baoyou'),
        ];
        return view('', $data);
    }


    //编辑
    public function edit( $id ){
        if( is_post() ){


            $data = input();  #post数据
            unset($data['pic']);
            unset($data['input-file']);
            
            
            if( isset($_FILES['pic']) ){
                $pic = com_up_pic('article', 'pic', true);
                $data['pic'] = $pic;
                com_check(['err'=>['js_action'=>'alert'], 'suc'=>['return_type'=>'return']] );
            }else if( isset($data['delete_pic']) ){
                $data['pic'] = '';
                unset($data['delete_pic']);
            }
            $data['id'] = $id;
            \app\admin\model\Goods::update($data);
            $config = [
                'suc' => [
                    'msg' => '编辑商品成功！',
                    'js_action' => 'alert|close',
                ],
            ];
            com_check( $config );
        }

        
        $rec    = new \app\admin\model\GoodsRec();
        $info   = \app\admin\model\Goods::get($id)->getData();
        $data = [
            'category'  => \app\admin\model\GoodsCat::allSubCat(),
            'rec'       => array_merge([0=>'无推荐位'], $rec->lists(0)),
            'status'    => c('type.status'),
            'is_baoyou' => c('type.is_baoyou'),
            'info'      => $info,
        ];
        return view('', $data);
    }


     //编辑阅读量
    public function editViews(){
        if( is_post() ){
            $data   = [
                'id'    => input('id'),
                'views' => (int)input('val'),
            ];
            \app\admin\model\Goods::update($data);
            $config = [
                'suc' => [
                    'msg' => '编辑浏览量成功！',
                    'js_action' => 'alert',
                ],
            ];
            com_check($config);
        }
    }
    
    
    //批量删除
    public function delall(){
        if( is_post() ){
            \app\admin\model\Goods::destroy( implode(",", input('ids/a')) );
            $config = [
                'suc' => [
                    'msg' => '删除成功！',
                    'js_action'=>"alert|reload",
                ],
            ];
            com_check($config);
        }
    }

  //编辑推荐位
    public function editRec(){
        if( is_post() ){
            $d   = [
                'id'        => input('id'),
                'rec_id'    => (int)input('val'),
            ];

            Db::name('goods')->update($d);

            $config = [
                'suc' => [
                    'msg' => '编辑推荐位成功！',
                    'js_action' => 'alert',
                ],
            ];
            com_check( $config );
        }
    }

	
}